package com.tanhua.server.service;

import cn.hutool.core.collection.CollUtil;
import com.tanhua.autoconfig.template.HuanXinTemplate;
import com.tanhua.commons.utils.Constants;
import com.tanhua.dubbo.api.FriendApi;
import com.tanhua.dubbo.api.UserApi;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.model.domain.User;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.mongo.Friend;
import com.tanhua.model.vo.ContactVo;
import com.tanhua.model.vo.ErrorResult;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.UserInfoVo;
import com.tanhua.server.exception.BusinessException;
import com.tanhua.server.interceptor.UserHolder;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sun.text.normalizer.CharTrie;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class MessagesService {
    @DubboReference
    private UserApi userApi;

    @DubboReference
    private UserInfoApi userInfoApi;

    @DubboReference
    private FriendApi friendApi;

    @Autowired
    private HuanXinTemplate huanXinTemplate;


    //根据环信用户id，查询用户详情
    public UserInfoVo findUserInfoByHuanxin(String huanxinId) {
        //1.根据环信id查询用户
        User user = userApi.findByHuanxin(huanxinId);

        //2.根据用户id，查询用户详情
        UserInfo userInfo = userInfoApi.findById(user.getId());

        //3.构建vo返回值
        //构建一个UserInfoVo对象
        UserInfoVo vo = new UserInfoVo();
        //copy同名类型属性 进行属性拷贝
        BeanUtils.copyProperties(userInfo, vo);
        //对于个性化属性，进行手动赋值
        if (userInfo.getAge() != null) {
            vo.setAge(userInfo.getAge().toString());
        }
        return vo;
    }

    //添加好友关系
    public void contacts(Long friendId) {
        //1.将好友关系注册到环信
        Boolean aBoolean = huanXinTemplate.addContact(Constants.HX_USER_PREFIX + UserHolder.getUserId(),
                Constants.HX_USER_PREFIX + friendId);
        if (!aBoolean) {
            throw new BusinessException(ErrorResult.error());
        }

        //2.如果注册成功，记录好友关系到mongodb  添加双向好友关系数据
        friendApi.save(UserHolder.getUserId(), friendId);

    }

    //分页查询联系人列表
    public PageResult findFriends(Integer page, Integer pagesize, String keyword) {
        //1.调用API查询当前用户的好友 list，封装了friend对象 仅仅包含了好友的id
        List<Friend> list = friendApi.findByUserId(UserHolder.getUserId(), page, pagesize);
        //判断列表是否为空，给前端返回一个空分页对象
        if(CollUtil.isEmpty(list)){
            return new PageResult();
        }

        //2.提取好友关系表中  数据列表中好友id，得到所有好友的用户id
        //参数：2.要提取的属性名 3.它的类型
        List<Long> userIds = CollUtil.getFieldValues(list, "friendId", Long.class);

        //3.调用UserInfoAPI查询好友的详细信息
        //条件
        UserInfo info = new UserInfo();
        info.setNickname(keyword);
        //第二个参数，是附加条件
        //筛选后的
        Map<Long, UserInfo> map = userInfoApi.findByIds(userIds, info);

        //4.构造vo对象，一个friend 构建一个vo对象
        //注意：这里不用map直接获取，是因为list是有序的，而map是无序的，所以通过list存
        List<ContactVo> vos = new ArrayList<>();
        for (Friend friend : list) {
            UserInfo userInfo = map.get(friend.getFriendId());
            if(userInfo != null){
                ContactVo vo = ContactVo.init(userInfo);
                vos.add(vo);
            }
        }
        return  new PageResult(page,pagesize,0l,vos);
    }
}
